package com.pansoft.openplanet.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.bean.AssetListBean;
import com.pansoft.openplanet.bean.BtcTxIO;
import com.pansoft.openplanet.constant.IPAssetsConstant;
import com.pansoft.openplanet.util.ChainParamsUtil;
import com.pansoft.openplanet.util.EosNetParamUtil;
import com.utilcode.util.TimeUtils;
import com.utilcode.util.ToastUtils;

import java.util.ArrayList;

import static com.efounder.frame.utils.Constants.CHAT_USER_ID;

public class TransactionResultActivity extends BaseActivity implements View.OnClickListener {

    private ImageView mIvStatus;
    private TextView mTvStatus;
    private TextView mTvTime;
    private TextView mTvAmountValue;
    private TextView mTvGasValue;
    private TextView mTvGasFormula;
    private TextView mTvReceiveAddress;
    private TextView mTvPayAddress;
    private TextView mTvNoteValue;
    private TextView mTvNumberValue;
    private TextView mTvBlockValue;
    private ImageView mIvBarCode;
    private TextView tvGas;

    String amount;//金额
    String note; //备注
    String receiveAddress;//收款地址
    String payAddress; //付款地址
    String gas = "";//转账费用
    String gasUnit = ""; //手续费单位
    AssetListBean assetListBean;
    boolean success;
    private String txHash;
    private int chainId;//BTC,ETH等
    private String moneyUnit;
    private ArrayList<BtcTxIO> btcInputs;
    private ArrayList<BtcTxIO> btcOutputs;

    private String copyAddressToast = ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_base_id_copied);
    private String copyTxHashToast = ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_transaction_number_copied);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_transaction_detail);
        initView();
        initIntnet();
        initData();
    }

    private void initView() {

        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(ResStringUtil.getString(R.string.open_planet_detail));
        mIvStatus = (ImageView) findViewById(R.id.iv_status);
        mTvStatus = (TextView) findViewById(R.id.tv_status);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvAmountValue = (TextView) findViewById(R.id.tv_amount_value);
        tvGas = (TextView) findViewById(R.id.tv_gas);
        mTvGasValue = (TextView) findViewById(R.id.tv_gas_value);
        mTvReceiveAddress = (TextView) findViewById(R.id.tv_receive_address);
        mTvReceiveAddress.setOnClickListener(this);
        mTvPayAddress = (TextView) findViewById(R.id.tv_pay_address);
        mTvPayAddress.setOnClickListener(this);
        mTvNoteValue = (TextView) findViewById(R.id.tv_note_value);
        mTvNumberValue = (TextView) findViewById(R.id.tv_number_value);
        mTvNumberValue.setOnClickListener(this);
        mTvBlockValue = (TextView) findViewById(R.id.tv_block_value);
        mIvBarCode = (ImageView) findViewById(R.id.iv_bar_code);
    }

    private void initIntnet() {
        chainId = getIntent().getIntExtra("chainId", IPAssetsConstant.CHAIN_ID_TURBOCHAIN);

        moneyUnit= ChainParamsUtil.getAssetUnitByChainId(chainId);

        gasUnit = moneyUnit;
        copyAddressToast= moneyUnit+ ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_address_copied);

//        if (chainId == IPAssetsConstant.CHAIN_ID_BTC) {
//            moneyUnit = "BTC";
//            copyAddressToast = "BTC" + ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_address_copied);
//            gasUnit = "btc";
//        } else if (chainId == IPAssetsConstant.CHAIN_ID_ETH) {
//            moneyUnit = "ETH";
//            gasUnit = "ETH";
//            copyAddressToast = "ETH" + ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_address_copied);
//        } else if (chainId == EosNetParamUtil.getEosChainId()) {
//            moneyUnit = "eos";
//            copyAddressToast = "EOS" + ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_address_copied);
//        } else if (chainId == IPAssetsConstant.CHAIN_ID_TURBOCHAIN) {
//            moneyUnit = "PWR";
//            copyAddressToast = ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_base_id_copied);
//            gasUnit = "PWR";
//        } else if (chainId == IPAssetsConstant.CHAIN_ID_ETE) {
//            moneyUnit = "ETE";
//            copyAddressToast = "ETE" + ResStringUtil.getString(R.string.open_planet_asset_mine_pwr_address_copied);
//        }

//        ChainParamsUtil.setTransactionResultHint(mode,moneyUnit,gasUnit,copyAddressToast);
        if (getIntent().hasExtra("moneyUnit")) {
            moneyUnit = getIntent().getStringExtra("moneyUnit");

        }
        amount = getIntent().getStringExtra("money");
        //屏蔽代码为多输入输出用
//        if (mode == IPAssetsConstant.CHAIN_ID_BTC) {
//            btcInputs = (ArrayList<BtcTxIO>) getIntent().getExtras().get("payAddress");
//            btcOutputs = (ArrayList<BtcTxIO>) getIntent().getExtras().get("receiveAddress");
//        }else {
        receiveAddress = getIntent().getStringExtra("receiveWalletAddress");
        payAddress = getIntent().getStringExtra("sendWalletAddress");
//        }
        txHash = getIntent().getStringExtra("tradingID");
        gas = getIntent().getStringExtra("txFee");
        note = getIntent().getStringExtra("note");
        success = getIntent().getBooleanExtra("success", true);
        assetListBean = (AssetListBean) getIntent().getSerializableExtra("assetBean");
    }

    private void initData() {
        mTvAmountValue.setText("- " + amount + " " + moneyUnit);
        //有这俩字段，是从transferItem转账消息过来的，按下面处理
        if (getIntent().hasExtra("sendIMUserId") || getIntent().hasExtra("receiveIMUserId")) {
            //发送方id等于我的id，是我发出的
            if (getIntent().getStringExtra("sendIMUserId").equals(EnvironmentVariable.getProperty(CHAT_USER_ID))) {
                mTvAmountValue.setText("- " + amount + moneyUnit);
                mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_out_success));
            } else {
                //发送方id不等于我的id，对方转入我的账户
                if (!getIntent().getStringExtra("sendIMUserId").equals("")) {
                    mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_in_success));
                    mTvAmountValue.setText("+ " + amount + moneyUnit);
                }
            }
            //若果接收方id等于我的，那么是我接收的
            if (getIntent().getStringExtra("receiveIMUserId").equals(EnvironmentVariable.getProperty(CHAT_USER_ID))) {
                mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_in_success));
                mTvAmountValue.setText("+ " + amount + moneyUnit);
            } else {
                //接收方id不是我的，那么是我发出的
                if (!getIntent().getStringExtra("receiveIMUserId").equals("")) {
                    mTvAmountValue.setText("- " + amount + moneyUnit);
                    mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_asset_transaction_result_out_success));
                }
            }
        }

        if (chainId == IPAssetsConstant.CHAIN_ID_BTC) {
            mTvGasValue.setText(gas);
//            StringBuilder builder = new StringBuilder();
//            for (BtcTxIO btcTxIO : btcOutputs) {
//                builder.append(btcTxIO.getAddress()).append("(")
//                        .append(btcTxIO.getValue()).append(")\n");
//            }
//            mTvReceiveAddress.setText(builder);
//            StringBuilder inBuilder = new StringBuilder();
//            for (BtcTxIO btcTxIO : btcInputs) {
//                inBuilder.append(btcTxIO.getAddress()).append("(")
//                        .append(btcTxIO.getValue()).append(")\n");
//            }
//            mTvPayAddress.setText(inBuilder);
        } else if (chainId == EosNetParamUtil.getEosChainId()) {
            mTvGasValue.setVisibility(View.GONE);
            tvGas.setVisibility(View.GONE);
        } else {
//            mTvGasValue.setText(gas + " " + moneyUnit);
            mTvGasValue.setText(gas + " " + gasUnit);
        }
        mTvReceiveAddress.setText(divideAddress(receiveAddress));
        mTvPayAddress.setText(divideAddress(payAddress));
//        mTvGasFormula = (TextView) findViewById(R.id.tv_gas_formula);
//        mTvGasFormula.setVisibility(View.INVISIBLE);
        if (getIntent().hasExtra("success") && success) {
            mIvStatus.setImageDrawable(getResources().getDrawable(R.drawable.transfer_success));
            mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_transfer_success));
        } else if (getIntent().hasExtra("success") && !success) {
            mIvStatus.setImageDrawable(getResources().getDrawable(R.drawable.transfer_fail));
            mTvStatus.setText(ResStringUtil.getString(R.string.open_planet_transfer_fail));
        }
        mTvNumberValue.setText(txHash);
        mTvNoteValue.setText(note);
        if (getIntent().hasExtra("tradingTime")) {
            mTvTime.setText(getIntent().getStringExtra("tradingTime"));
        } else {
            mTvTime.setText(TimeUtils.getNowString());
        }
        mTvBlockValue.setText(getIntent().getStringExtra("block"));

//        Bitmap bm = QRCode.setQRcode(queryUrl, 800, 800);
//        if (bm != null) {
//            mIvBarCode.setImageBitmap(bm);
//        }
    }

    /**
     * 将地址分为两行显示
     *
     * @param address 地址
     * @return address
     */
    private String divideAddress(String address) {
        if (chainId == EosNetParamUtil.getEosChainId()) {
            return address;
        }
        return address.substring(0, address.length() / 2) + "\n"
                + address.substring(address.length() / 2, address.length());
    }

    private void copyAddress(String address, String toast) {
        //复制账号
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        cm.setText(address);
        ToastUtils.setBgColor(getResources().getColor(R.color.black));
        ToastUtils.setMsgColor(getResources().getColor(R.color.op_text_white_color));
        ToastUtils.showShort(toast);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_receive_address) {
            copyAddress(mTvReceiveAddress.getText().toString().replaceAll("\n", ""),
                    copyAddressToast);
        } else if (id == R.id.tv_pay_address) {
            copyAddress(mTvPayAddress.getText().toString().replaceAll("\n", ""),
                    copyAddressToast);
        } else if (id == R.id.tv_number_value) {
            copyAddress(txHash, copyTxHashToast);
        }
    }
}
